﻿#region Using namespaces

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Xml.Linq;

using BOL.IO;
using BOL.Apps.GeneNetworkBrowser.Analysis;
using BOL.Apps.GeneNetworkBrowser.ViewModels;

#endregion

namespace BOL.Apps.GeneNetworkBrowser.IO
{
    public sealed class ProjectReader : IReader<ProjectTreeViewItemViewModel>
    {
        #region Private variables

        private FileStream _fs;

        #endregion

        #region Constructors

        public ProjectReader(FileStream fs)
        {
            _fs = fs;
        }

        public ProjectReader(string path) : this(new FileStream(path, FileMode.Open)) { } 

        #endregion

        #region IReader implementation

        public ProjectTreeViewItemViewModel Read()
        {
            XDocument xd = XDocument.Load(_fs);

            var name = xd.Descendants("Name").First().Value.Trim();
            var networks = new List<Network>();
            var analyses = new List<IAnalysis>();
            //foreach (var network in xd.Descendants("NetworkGroup"))
            //    networks.Add(network.Value.Trim());
            //foreach (var analysis in xd.Descendants("AnalysisGroup"))
            //    analyses.Add(analysis.Value.Trim());

            return new ProjectTreeViewItemViewModel(name, networks, analyses);
        }

        public void Close()
        {
            _fs.Close();
        }

        #endregion
    }
}
